- /* dbl2lne.cpp by K.Tsuru */
- /****************************
- SN class's auxiliary function
- *****************************/
- #ifndef SN_H
- #include "sn.h"
- #endif
- /*-----------------------------------------------------------------------------
- Try to convert double "d" using long "L" into a form
- d = L * 10^e ( 0 =< |L| <= mt).
- When it is successed, return true. The operation between SDouble and double can
- be reduced into one between SDouble and short integer.
- e.g. x/1.3 ---> (x/13000)*DRADIX.
- Usually take mt = (double)d.SlOpMaxValue() (= ULONG_MAX/radix);
- -------------------------------------------------------------------------------*/
- bool doubleTolongExp(double d, long* L, int* e, double mt){
- // if(fabs(d) > mt) return false;
- if(d == 0.0){
- *L = 0; *e = 0; return true;
- }
- FigBlock res;
- uint sz;
- int exp, j;
- exp = doubleToArray(d, res, &sz);
- if(sz > 4) return false; // 0.0001 2345 6789 1000 (ten figures sz = 5) > SlOpMaxValue()
- double rdx = DRADIX, r = res(sz-1);
-
- for(j = (int)sz-2; j >= 1; j--){
- r += res(j)*rdx;
- rdx *= DRADIX;
- }
- exp = (exp - (int)sz +1)*DFIGURES;
- if(r > mt){ //It is desirable that "exp" is a multiple of "DFIGURES" if possible.
- while( fmod(r, 10.0) < DBL_EPSILON){ //remove lower zeros
- exp++; r /= 10.0;
- // if( !( abs(exp) % DFIGURES ) && (r < mt) ) break;
- }
- }
- *e = exp;
- r += ROUND_DBL_INT;
- if(r > mt) return false; //fail to convert
- // convert to long
- *L = (long)r;
- if(d < 0.0) *L = -*L;
- return true;
- }
dbl2lne.cpp : last modifiled at 2015/09/06 13:22:47(1,522 bytes)
created at 2016/04/11 11:17:20
The creation time of this html file is 2017/10/07 10:54:15 (Sat Oct 07 10:54:15 2017).